<?php
namespace app\index\controller;
use think\Db;
use think\Controller;
use  think\Request;
use think\Config;

class User extends Base
{
    
	public function Index(){
		
		
		
		
		
		 return view();
	}
	
	
	
	function Add(){
		
		$request = Request::instance();
	
		
		if($request->isPost()){
				$data["id"] =$request->post('id');
			
				$data["truename"] =$request->post('truename');
				$data["createtime"] =  date("Y-m-d h:i:sa");
				
			
				
			if($data["id"]){
				
			
				
				if(db('users')->update($data)){
					
					  return json(['status'=>200,'message'=>'修改成功']);
				}else{
					 return json(['status'=>500,'message'=>'修改失败']);
				}
			}else{
				$data["phone"] =$request->post('phone');
				$data["mid"] = session("mid");
				$data["type"] = "1";
			  $user =  db("users")->where("phone='".$data["phone"]."'")->find();
			  if($user){
					return json(['status'=>500,'message'=>'手机号重复']);

			  }else{

				if(db('users')->insertGetId($data)){
					return json(['status'=>200,'message'=>'添加成功']);
				   
			   }else{
					return json(['status'=>500,'message'=>'添加失败']);
			   }

			  }
			  	
			
			}
			
		}else{
			
			if($request->get('id')){
				$info =db('users')->where("id=".$_GET["id"])->find();
				$this->assign('info',$info);
				
			}
			
			
		
		
	
		
			
		}
		
		
	
	
		return view();
	}
	 public function GetUser(){
		$where="mid=".session("mid")." and type=1";
		$count =db('users')->where($where)->count();
		$first = $_GET["size"] * ($_GET["page"] -1);
		 
		$list = db('users')->where($where)->order("id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
			$students = db("studentandparent")->where("userid=".$value["id"])->select();
		
			foreach ($students as $key1 => &$value1) {
				$student = db("student")->where("id=".$value1["studentid"])->find();
				if($student){
					$value1["truename"]=$student["truename"];
				}else{
					$value1["truename"]= "已经不存在";
				}
				
			}
			$value["students"]= $students;
			
			
			
		}
		
		$obj["count"]=(int)($count/$_GET["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);
		
	}
	public function GetUserList(){
		$where="mid=".session("mid")." and type=1";
	
	
		 
		$list = db('users')->where($where)->select();
		foreach ($list as $key => &$value) {
			$students = db("studentandparent")->where("userid=".$value["id"])->select();
			
			foreach ($students as $key1 => &$value1) {
				$student = db("student")->where("id=".$value1["studentid"])->find();
				$value1["truename"]=$student["truename"];
			}
			$value["students"]= $students;
			
			
			
		}
		
	

		return json($list);
		
	}
	
	function Del(){
		$request = Request::instance();
		$id =$request->post('id');
		
		db('studentandparent')->where("userid=".$id)->delete();
		 
			$where = 'id='.$id;
		  
		   $list=db('users')->where($where)->delete();
		   if($list!==false) {
			 return ['status'=>200,'message'=>'删除成功'];
		   }else{
			  return ['status'=>500,'message'=>'删除失败'];
		   }
		
	}
	function jiebang(){
		$request = Request::instance();
		$id =$request->post('id');

		$where="id=".$id;
		$list=db('studentandparent')->where($where)->delete();
		if($list!==false) {
			return ['status'=>200,'message'=>'操作成功'];
		  }else{
			 return ['status'=>500,'message'=>'操作失败'];
		  }
	}
	function vip(){
		$request = Request::instance();
		$mid = session("mid");
		$where="id=".$mid;
		$mechanism=db("mechanism")->where($where)->find();
		if($request->isPost()){
			$studentlist = $request->post("studentlist");
			$month = $request->post("month");
			$model = $request->post("model");
			$money = $request->post("money");

			$arr = explode(",",$studentlist);
			$length = sizeof($arr);

			$summoney = $length * $money * $month;
			
		

			$daili=db("daili")->where("id=".$mechanism["dailiid"])->find();
			$dailisummoney=0;
			$ddsummoney=0;
			if($daili){
					$dailimymoney = $daili["mymoney"];
			
					
					if($model=="1"){
						$dailisummoney =  $length * $daili["model1"] * $month;
						$ddsummoney =  $daili["model1"] * $month;
					}else if($model=="2"){
						$dailisummoney =  $length * $daili["model2"] * $month;
						$ddsummoney =  $daili["model2"] * $month;
					}
					$mymoney =$mechanism["mymoney"];
					if($summoney>$mymoney){
						return ['status'=>400,'message'=>'机构余额不足，还差'.($summoney-$mymoney)."元"];
					}
					if($dailisummoney>$dailimymoney){
						return ['status'=>400,'message'=>'机构所属代理余额不足，还差'.($dailisummoney-$dailimymoney)."元"];
					}

			}else{
				$mymoney =$mechanism["mymoney"];
				if($summoney>$mymoney){
					return ['status'=>400,'message'=>'机构余额不足，还差'.($summoney-$mymoney)."元"];
				}
			}
		
			

			for($i=0;$i<$length;$i++){
		
				$student = db("student")->where("id=".$arr[$i])->find();
				$data["studentid"]=$arr[$i];
				$data["month"]=$month;
				$data["money"]=$money;
				$data["summoney"]=$money * $month;
				$data["dailisummoney"]=$ddsummoney;
				$data["createtime"]=date("Y-m-d h:i:sa");
				$data["mid"]=session("mid");

				if(db("studentvideo")->insertGetId($data)){
					if($student){
						if($student["videoetime"]!=""){
							
							$videoetime = date("Y-m-d", strtotime("+".$month." months", strtotime($student["videoetime"])));
	
						}else{
							$videoetime = date("Y-m-d", strtotime("+".$month." months", strtotime(date("Y-m-d h:i:sa"))));
						}

					   Db::execute('update student set videoetime=?,modeltype=? where id=?',[$videoetime,$model,$arr[$i]]);

					}

				}
				
			}
			//减去机构金额
			$result1 = Db::execute('update mechanism set mymoney=mymoney-? where id=?',[$summoney,$mid]);
			if($daili){
				//减去代理金额
					$result2 =Db::execute('update daili set mymoney=mymoney-? where id=?',[$dailisummoney,$daili["id"]]);
			}else{
				$result2=true;
			}
		
			if($result1&&$result2){
				return ['status'=>200,'message'=>'操作成功'];
			}else{
				return ['status'=>400,'message'=>'操作失败'];
			}

		
		}else{



			$where="mid=".session("mid");
			$myclass = db('myclass')->where($where)->select();
			$config = db("sys_webconfig")->where("mid=0")->find();
			$model1="0";
			$model2="0";
			
			if($mechanism["model1"]==""){
				$model1=$config["model1"];
			}else{
				$model1=$mechanism["model1"];
			}
			if($mechanism["model2"]==""){
				$model2=$config["model2"];
			}else{
				$model2=$mechanism["model2"];
			}
		
			$arr = [];
			$obj1["id"]="1";
			$obj1["title"]="签到";
			$obj1["money"]=$model1;

			$obj2["id"]="2";
			$obj2["title"]="签到+视频";
			$obj2["money"]=$model2;

			array_push($arr,$obj1);
			array_push($arr,$obj2);


			$this->assign('config',$arr);
			//print_r($myclass);
			$this->assign('myclass',$myclass);
			return view();
	}
}


	function order(){

		return view();
	}
	function getorderlist(){
		$where="studentvideo.mid=".session("mid");

		if($_POST["truename"]){
			$where=$where." and student.truename like '%".$_POST["truename"]."%'";

		}else if($_POST["classid"]){
			$where=$where." and student.classid=".$_POST["classid"];

		}else if($_POST["stime"]!=""&&$_POST["etime"]!=""){
			$stime  =$_POST["stime"]." 00:00:00";
			$etime = $_POST["etime"]." 23:59:59";
			$where=$where." and studentvideo.createtime between '".$stime."' and '".$etime."'";
		}

		$count =db('studentvideo')->join("student","student.id=studentvideo.studentid")->join("myclass","student.classid=myclass.id")->where($where)->count();
		$first = $_POST["size"] * ($_POST["page"] -1);
		 
		$list = db('studentvideo')->join("student","student.id=studentvideo.studentid")->join("myclass","student.classid=myclass.id")
		->where($where)->field("studentvideo.*,student.truename as studentname,myclass.classname,student.videoetime")->order("studentvideo.id desc")->limit($first,$_GET["size"])->select();
		foreach ($list as $key => &$value) {
		
		}
		
		$obj["count"]=(int)($count/$_POST["size"])+1;
		$obj["allcount"]=$count;
		$obj["list"]=$list;
		return json($obj);

	}
	function orderdel(){
		$request = Request::instance();
		$id =$request->post('id');

		$where="id=".$id;
		$list=db('studentvideo')->where($where)->delete();
		if($list!==false) {
			return ['status'=>200,'message'=>'操作成功'];
		  }else{
			 return ['status'=>500,'message'=>'操作失败'];
		  }
		
	}
}
